$(document).ready(function () {
	//定义全局对象
	var phone_reg = /(^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$)|(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/;//姓名/手机号 手机号验证

	//时间格式化函数
	Date.prototype.Format = function (fmt) { //author: meizz
		var o = {
			"M+": this.getMonth() + 1, //月份
			"d+": this.getDate(), //日
			"h+": this.getHours(), //小时
			"m+": this.getMinutes(), //分
			"s+": this.getSeconds(), //秒
			"q+": Math.floor((this.getMonth() + 3) / 3), //季度
			"S": this.getMilliseconds() //毫秒
		};
		if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (var k in o)
			if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
		//调用  new Date().Format("yyyy-MM-dd hh:mm:ss");
	};

	//切换类型  普通、黄金 、白金、钻石
	switch_status();
	function switch_status() {
		var recharge_status_general = $('.recharge_status_general');//普通-项目
		var jollyone_general = $('#jollyone_general');//普通-jolly
		var experience_general = $('#experience_general');//普通-体验
		var recharge_status_gold = $('.recharge_status_gold');//黄金
		var recharge_status_platina = $('.recharge_status_platina');//白金
		var recharge_status_jewel = $('.recharge_status_jewel');//钻石
		var recharge_status_mouth = $('.recharge_status_mouth');//口腔
		var select_project = $('#select_project');//项目  商品  体验券
		$('.recharge_status').delegate('.text-center','click',function () {
			var span = $(this).find('span');
			$('.recharge_status').find('span').removeClass('active');//删除所有活动类
			span.addClass('active');//当前点击项添加活动类

			if(span.text() == '普通项目'){
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.removeClass('active');//隐藏口腔
				select_project.removeClass('active');

				if( select_project.find('.active').text() == '项目' ){
					jollyone_general.removeClass('active');//隐藏普通-jolly
					experience_general.removeClass('active');//隐藏普通-体验
					recharge_status_general.addClass('active');//展示普通-项目
				}

				if( select_project.find('.active').text() == '商品' ){
					experience_general.removeClass('active');//隐藏普通-体验
					recharge_status_general.removeClass('active');//隐藏普通-项目
					jollyone_general.addClass('active');//展示普通-jolly
				}

				if( select_project.find('.active').text() == '体验券' ){
					jollyone_general.removeClass('active');//隐藏普通-jolly
					recharge_status_general.removeClass('active');//隐藏普通-项目
					experience_general.addClass('active');//展示普通-体验
				}
			}

			if(span.text() == '黄金项目'){
				recharge_status_general.removeClass('active');//隐藏普通-项目
				jollyone_general.removeClass('active');//隐藏普通-jolly
				experience_general.removeClass('active');//隐藏普通-体验
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_gold.addClass('active');//展示黄金
				select_project.addClass('active');
			}

			if(span.text() == '白金项目'){
				recharge_status_general.removeClass('active');//隐藏普通-项目
				jollyone_general.removeClass('active');//隐藏普通-jolly
				experience_general.removeClass('active');//隐藏普通-体验
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_platina.addClass('active');//展示白金
				select_project.addClass('active');
			}

			if(span.text() == '钻石项目'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_general.removeClass('active');//隐藏普通-项目
				jollyone_general.removeClass('active');//隐藏普通-jolly
				experience_general.removeClass('active');//隐藏普通-体验
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_mouth.removeClass('active');//隐藏口腔
				recharge_status_jewel.addClass('active');//展示钻石
				select_project.addClass('active');
			}

			if(span.text() == '口腔项目'){
				recharge_status_general.removeClass('active');//隐藏普通
				recharge_status_general.removeClass('active');//隐藏普通-项目
				jollyone_general.removeClass('active');//隐藏普通-jolly
				experience_general.removeClass('active');//隐藏普通-体验
				recharge_status_gold.removeClass('active');//隐藏黄金
				recharge_status_platina.removeClass('active');//隐藏白金
				recharge_status_jewel.removeClass('active');//隐藏钻石
				recharge_status_mouth.addClass('active');//展示口腔
				select_project.addClass('active');
			}

		});
	}

	//设置滑动列表高度  超出滑动
	set_height_overflow('jollyone_general');
	set_height_overflow('experience_general');
	set_height_overflow('recharge_status_general');
	set_height_overflow('recharge_status_gold');
	set_height_overflow('recharge_status_platina');
	set_height_overflow('recharge_status_jewel');
	set_height_overflow('recharge_status_mouth');
	function set_height_overflow(contentId) {
		var con = $('#' + contentId);//超出滑动容器
		var recharge_status = $('.recharge_status');
		var nav = $('.nav');
		var select_project = $('#select_project');

		if( contentId == 'jollyone_general' || contentId == 'recharge_status_general' || contentId == 'experience_general' ){
			con.css('maxHeight', $(window).height() - recharge_status.height() - nav.outerHeight() - select_project.height() );
		}
		else {
			con.css('maxHeight', $(window).height() - recharge_status.height() - nav.outerHeight() );
		}
		con.css('overflow', 'auto' );
	}

	//选择产品类型
	select_jollyone_project();
	function select_jollyone_project() {
		var select = $('#select_project');
		var jollyone = $('#jollyone_general');
		var general = $('#recharge_status_general');
		var experience = $('#experience_general');

		select.delegate('.pull-left','click',function () {
			select.find('.active').removeClass('active');
			$(this).addClass('active');

			if( $(this).text() == '项目' ){
				jollyone.removeClass('active');
				experience.removeClass('active');
				general.addClass('active');
			}

			if( $(this).text() == '商品' ){
				general.removeClass('active');
				experience.removeClass('active');
				jollyone.addClass('active');
			}

			if( $(this).text() == '体验券' ){
				general.removeClass('active');
				jollyone.removeClass('active');
				experience.addClass('active');
			}
		});
	}

	//获取jollyone订单列表并展示
	get_pt_jollyone_list('jollyone_general');
	function get_pt_jollyone_list(content) {
		$.ajax({
			url: '/Jolly/order_list',
			type:'POST',
			dataType: 'json',
			data: {},
			success:function (data) {
				if(data.data){
					$('#' + content).empty();
					create_pt_jollyone_append_page( content , data.data );//jollyone列表生成并插入到页面
				}else {
					var html = '';
					html += '<div style="background-color: #f2f2f2;padding-top: 5rem;">';
					html += '<img style="width: 20%;margin-left: 40%" src="/statics/images/empty_project.png" alt="">';
					html += '<div class="text-center" style="font-size: 0.4rem;color: #999;">暂无订单</div>';
					html += '</div>';
					$('#' + content).html( html )
				}
			},
			error:function (err) {
				console.log(err);
			}
		})
	}

	//jollyone订单列表生成并插入到页面
	function create_pt_jollyone_append_page( content ,data) {
		var general = $('#' + content);//jollyone项目列表
		for( var i = 0 , y = data.length ; i < y ; i++ ){
			var img = '';
			var status = '';
			var order_status = '';
			var text = '';
			if( data[i].jolly_face_img == '' || data[i].jolly_face_img == null ){
				img = '/statics/images/u=4014792626,4066954880&fm=27&gp=0.jpg';
			}else {
				img = data[i].jolly_face_img;
			}

			/*
			* order_status:-1、取消  0、待发货 1、已发货  2、已完成
			* */
			if( data[i].order_status == '-1' ){
				status = 'active';
				order_status = 'define_active';
				text = '已取消';
			}
			if( data[i].order_status == '0' ){
				status = '';
				order_status = 'active';
				text = '待发货';
			}
			if( data[i].order_status == '1' ){
				status = '';
				order_status = '';
				text = '已发货';
			}
			if( data[i].order_status == '2' ){
				status = 'active';
				order_status = 'active';
				text = '已完成';
			}
			var html = '';
			if( i == ( y - 1 ) ){
				html += '<div class="jollyone_item '+ order_status +'">';
				html += '<div class="clearfix">';
				html += '<div class="pull-left"><img src="'+ img +'" alt=""></div>';
				html += '<div class="pull-left">';
				html += '<div>'+ data[i].jolly_name +'</div>';
				html += '<div>价值'+ data[i].jolly_price +'积分</div>';
				html += '</div>';
				html += '<div class="status '+ status +'">'+ text +'</div>';
				html += '</div>';
				html += '<div class="clearfix look_detial">';
				html += '<div class="pull-right meal_content" order_id="'+ data[i].order_id +'">确认收货</div>';
				html += '<div class="pull-right click_btn">订单详情</div>';
				html += '<div class="pull-right cancel_order" order_id="'+ data[i].order_id +'">取消订单</div>';
				html += '</div>';
				html += '</div>';
			}else {
				html += '<div class="jollyone_item '+ order_status +'">';
				html += '<div class="clearfix">';
				html += '<div class="pull-left"><img src="'+ img +'" alt=""></div>';
				html += '<div class="pull-left">';
				html += '<div>'+ data[i].jolly_name +'</div>';
				html += '<div>价值'+ data[i].jolly_price +'积分</div>';
				html += '</div>';
				html += '<div class="status '+ status +'">'+ text +'</div>';
				html += '</div>';
				html += '<div class="clearfix look_detial">';
				html += '<div class="pull-right meal_content" order_id="'+ data[i].order_id +'">确认收货</div>';
				html += '<div class="pull-right click_btn">订单详情</div>';
				html += '<div class="pull-right cancel_order" order_id="'+ data[i].order_id +'">取消订单</div>';
				html += '</div>';
				html += '<div style="height: 0.26666rem;background-color: #f2f2f2"></div>';
				html += '</div>';
			}

			general.append( html );
		}
	}

	//jollyone  点击订单详情跳转页面
	jollyone_click_click_btn();
	function jollyone_click_click_btn() {
		$('#jollyone_general').delegate('.click_btn','click',function () {
			window.location = '/index/user_order_info?order_id=' + $(this).prev().attr('order_id');
		});
	}

	//jollyone  点击"取消订单" 展示模态框
	jollyone_click_cancel_order();
	function jollyone_click_cancel_order() {
		var cancel_order = $('#cancel_order');//取消订单modal
		var jollyone_general = $('#jollyone_general');

		jollyone_general.delegate('.cancel_order','click',function () {
			cancel_order.modal();

			jollyone_click_cancel_order_ajax($(this),cancel_order);//jollyone  点击"取消订单"模态框确定按钮 ajax请求 刷新页面
		});
	}

	//jollyone  点击"取消订单"模态框确定按钮 ajax请求 刷新页面
	function jollyone_click_cancel_order_ajax(btn,cancel_order) {
		var sure_cancel_btn = cancel_order.find('div[data-dismiss="modal"]');

		sure_cancel_btn.on('click',function () {
			$.ajax({
				url: '/jolly/jolly_cancel',
				type:'POST',
				dataType: 'json',
				data: {
					order_id: btn.attr('order_id')
				},
				success:function (data) {
					if( data.code == '1' ){
						get_pt_jollyone_list('jollyone_general');//获取jollyone订单列表并展示
						btn.hide();
					}else {
						console.log(data.msg);
					}
				},
				error:function (err) {
					console.log(err);
				}
			})
		});
	}

	//jollyone  点击确认收货 调取确认收货接口
	jollyone_click_meal_content();
	function jollyone_click_meal_content() {
		var modal = $('#myModal');
		$('#jollyone_general').delegate('.meal_content','click',function () {
			var self = $(this);
			$.ajax({
				url: '/Jolly/confirm_order',
				type:'POST',
				dataType: 'json',
				data: {
					order_id: $(this).attr('order_id')
				},
				success:function (data) {
					if( data.code == '1' ){
						modal.find('.modal-body').text('收货成功！');
						modal.modal();

						//修改订单状态
						self.parents('.jollyone_item').addClass('active');
						self.parents('.jollyone_item').find('.status').text('已完成');
						self.parents('.jollyone_item').find('.status').addClass('active');
					}
				},
				error:function (err) {
					console.log(err);
				}
			})
		});
	}

	//获取订单列表
	get_order_list('recharge_status_general','1');//普通项目
	get_order_list('recharge_status_gold','5');//黄金项目
	get_order_list('recharge_status_platina','2');//白金项目
	get_order_list('recharge_status_jewel','3');//钻石项目
	get_order_list('experience_general','4');//体验券
	get_order_list('recharge_status_mouth','6');//口腔项目
	function get_order_list(content,score_type) {
		$.ajax({
			url: '/index/orderList',
			type:'GET',
			dataType: 'json',
			data: {
				score_type: score_type
			},
			success:function (data) {
				/*
				* is_used: 1、已消费  0、未消费
				* */
				if(data.data){
					$('#' + content).empty();
					create_order_list_append_page( content , data.data );//普通项目列表生成并插入到页面
				}else {
					var html = '';
					html += '<div style="background-color: #f2f2f2;padding-top: 5rem;">';
					html += '<img style="width: 20%;margin-left: 40%" src="/statics/images/empty_project.png" alt="">';
					html += '<div class="text-center" style="font-size: 0.4rem;color: #999;">暂无订单</div>';
					html += '</div>';
					$('#' + content).html( html )
				}
			},
			error:function (err) {
				console.log(err);
			}
		})
	}
	
	//生成订单列表并插入到页面
	function create_order_list_append_page( content , data ) {
		var general = $('#' + content);//普通项目列表
		for( var i = 0 , y = data.length ; i < y ; i++ ){
			var img = '';
			var status = '';
			var status_text = '';
			var text = '';
			if( data[i].cover_img == '' || data[i].cover_img == null ){
				img = '/statics/images/u=4014792626,4066954880&fm=27&gp=0.jpg';
			}else {
				img = data[i].cover_img;
			}

			/*
			* is_used: 1、已消费（无active）  0、未消费( 有立即消费按钮  有active)
			* delete_at 已取消
			* */
			if( data[i].delete_at != '0' && data[i].delete_at != '' && data[i].delete_at != null && data[i].delete_at != undefined ){
				status = 'define_active';
				text = '消费';
				status_text = '已取消';
			}else {
				if( data[i].is_used == '0' ){
					status = 'active';
					text = '消费';
					status_text = '';
				}
				if( data[i].is_used == '1' ){
					status = '';
					text = '查看详情';
					status_text = '已消费';
				}
			}

			var html = '';
			if( i == ( y - 1 ) ){
				html += '<div class="'+ status +'">';
				html += '<div class="clearfix">';
				html += '<div class="pull-left">订单号：</div>';
				html += '<div class="pull-left">'+ data[i].order_id +'</div>';
				html += '<div class="pull-right">'+ status_text +'</div>';
				html += '</div>';
				html += '<div class="clearfix">';
				html += '<div class="pull-left"><img src="'+ img +'" alt=""></div>';
				html += '<div class="pull-left">';
				html += '<div>'+ data[i].project_name +'</div>';
				html += '<div>价值'+ data[i].order_amount +'积分</div>';
				html += '</div>';
				html += '</div>';
				html += '<div class="clearfix look_detial">';
				html += '<div class="pull-left"><span>购买时间：</span><span style="font-size: 12px;">'+ data[i].add_at +'</span></div>';
				html += '<div class="pull-right click_btn" order_id="'+ data[i].order_id +'" detail_modal_name="'+ data[i].consumer +'" detail_modal_phone="'+ data[i].consumer_mobile +'" detail_modal_time="'+ data[i].update_at +'" >'+ text +'</div>';
				html += '<div class="pull-right click_cancel" order_id="'+ data[i].order_id +'">取消</div>';
				html += '<div class="pull-right meal_content" order_id="'+ data[i].order_id +'" project_id="'+ data[i].project_id +'">套餐内容</div>';
				html += '</div>';
				html += '</div>';
			}else {
				html += '<div class="'+ status +'">';
				html += '<div class="clearfix">';
				html += '<div class="pull-left">订单号：</div>';
				html += '<div class="pull-left">'+ data[i].order_id +'</div>';
				html += '<div class="pull-right">'+ status_text +'</div>';
				html += '</div>';
				html += '<div class="clearfix">';
				html += '<div class="pull-left"><img src="'+ img +'" alt=""></div>';
				html += '<div class="pull-left">';
				html += '<div>'+ data[i].project_name +'</div>';
				html += '<div>价值'+ data[i].order_amount +'积分</div>';
				html += '</div>';
				html += '</div>';
				html += '<div class="clearfix look_detial">';
				html += '<div class="pull-left"><span>购买时间：</span><span style="font-size: 12px;">'+ data[i].add_at +'</span></div>';
				html += '<div class="pull-right click_btn" order_id="'+ data[i].order_id +'" detail_modal_name="'+ data[i].consumer +'" detail_modal_phone="'+ data[i].consumer_mobile +'" detail_modal_time="'+ data[i].update_at +'" >'+ text +'</div>';
				html += '<div class="pull-right click_cancel" order_id="'+ data[i].order_id +'">取消</div>';
				html += '<div class="pull-right meal_content" order_id="'+ data[i].order_id +'" project_id="'+ data[i].project_id +'">套餐内容</div>';
				html += '</div>';
				html += '<div style="height: 0.26666rem;background-color: #f2f2f2"></div>';
				html += '</div>';
			}


			general.append( html );
		}
	}

	//订单列表  点击查看详情\立即消费  展示模态框
	click_look_detail('experience_general');//体验券
	click_look_detail('recharge_status_general');//普通项目
	click_look_detail('recharge_status_gold');//黄金项目
	click_look_detail('recharge_status_platina');//白金项目
	click_look_detail('recharge_status_jewel');//钻石项目
	click_look_detail('recharge_status_mouth');//口腔项目
	function click_look_detail(recharge_status) {
		var general = $('#' + recharge_status);//普通项目列表
		var detail_modal = $('#detail_modal');//查看详情modal
		var consume_modal = $('#consume_modal');//立即消费modal
		general.delegate('.click_btn','click',function () {
			var self = $(this);
			if( $(this).text() == '查看详情' ){
				show_modal( detail_modal );//show  模态框  查看详情
				detail_modal.find('.detail_modal_name').text( self.attr('detail_modal_name') );
				detail_modal.find('.detail_modal_phone').text( self.attr('detail_modal_phone') );
				detail_modal.find('.detail_modal_time').text( new Date( self.attr('detail_modal_time') * 1000 ).Format("yyyy年MM月dd日") );
			}
			if( $(this).text() == '消费' ){
				show_modal( consume_modal );//show  模态框  立即消费
				consume_modal.find('.consume_modal_time').text( new Date().Format("yyyy年MM月dd日") );
				consume_modal.find('input[name="order_id"]').val( self.attr('order_id') );
			}
			click_at_consume();//点击购买模态框中立即消费按钮 发起请求 返回结果  ajax
		})
	}

	//点击购买模态框中立即消费按钮 发起请求 返回结果  ajax
	function click_at_consume() {
		var consume_modal = $('#consume_modal');//立即消费modal
		var myModal = $('#myModal');//提示modal
		var recharge_status = $('.recharge_status');//普通、白金、钻石项目
		$('#at_consume').unbind('click');
		$('#at_consume').on('click',function () {
			var self = $(this);
			myModal.find('div[data-dismiss="modal"]').removeClass('active');
			var class_room = consume_modal.find('input[name="class_room"]');//课室
			var servicer = consume_modal.find('input[name="servicer"]');//服务商
			var counselor = consume_modal.find('input[name="counselor"]');//咨询师
			// var operate_nurse = consume_modal.find('input[name="operate_nurse"]');//操作护士
			var operate_place = consume_modal.find('input[name="operate_place"]');//操作地点
			var name = consume_modal.find('input[name="consumer"]');//姓名
			var phone = consume_modal.find('input[name="consumer_mobile"]');//手机号
			if( name.val() && phone_reg.test( phone.val() ) ){
				$.ajax({
					url: '/index/useCard',
					type:'POST',
					dataType: 'json',
					data: $('#consume_modal_form').serialize(),
					success:function (data) {
						myModal.find('.modal-body').text( data.msg );
						myModal.modal({keyboard:false});
						myModal.find('div[data-dismiss="modal"]').addClass('active');
						myModal.on('hide.bs.modal',function () {
							if( self.parents('.modal_define').hasClass('active') ){
								hide_modal( self.parents('.modal_define') );//hide  模态框
								//获取订单列表
								if( recharge_status.find('.active').text() == '普通项目' ){
									get_order_list('recharge_status_general','1');//普通项目
								}
								if( recharge_status.find('.active').text() == '黄金项目' ){
									get_order_list('recharge_status_platina','5');//黄金项目
								}
								if( recharge_status.find('.active').text() == '白金项目' ){
									get_order_list('recharge_status_platina','2');//白金项目
								}
								if( recharge_status.find('.active').text() == '钻石项目' ){
									get_order_list('recharge_status_jewel','3');//钻石项目
								}
								if( recharge_status.find('.active').text() == '体验券' ){
									get_order_list('experience_general','4');//体验券
								}
								if( recharge_status.find('.active').text() == '口腔项目' ){
									get_order_list('experience_general','6');//口腔项目
								}
								name.val('');
								phone.val('');
							}
						})
					},
					error:function (err) {
						console.log(err);
					}
				})
			}else {
				if( !class_room.val() ){
					myModal.find('.modal-body').text('请填写课室');
					myModal.modal({keyboard:false});
					return;
				}

				if( !servicer.val() ){
					myModal.find('.modal-body').text('请填写服务商');
					myModal.modal({keyboard:false});
					return;
				}

				if( !counselor.val() ){
					myModal.find('.modal-body').text('请填写咨询师');
					myModal.modal({keyboard:false});
					return;
				}

				/*if( !operate_nurse.val() ){
					myModal.find('.modal-body').text('请填写操作护士');
					myModal.modal({keyboard:false});
					return;
				}*/

				if( !operate_place.val() ){
					myModal.find('.modal-body').text('请填写操作地点');
					myModal.modal({keyboard:false});
					return;
				}

				if( !name.val() ){
					myModal.find('.modal-body').text('请填写您的姓名');
					myModal.modal({keyboard:false});
					return;
				}

				if( !phone.val() ){
					myModal.find('.modal-body').text('请填写您的手机号');
					myModal.modal({keyboard:false});
					return;
				}

				if( phone.val() && !phone_reg.test( phone.val() ) ){
					myModal.find('.modal-body').text('请您填写正确11位手机号');
					myModal.modal({keyboard:false});
					return;
				}
			}
		})
	}

	//订单列表  点击查看取消  展示模态框
	click_click_cancel('experience_general');//体验券
	click_click_cancel('recharge_status_general');//普通项目
	click_click_cancel('recharge_status_gold');//黄金项目
	click_click_cancel('recharge_status_platina');//白金项目
	click_click_cancel('recharge_status_jewel');//钻石项目
	click_click_cancel('recharge_status_mouth');//口腔项目
	function click_click_cancel(recharge_status) {
		var general = $('#' + recharge_status);//普通项目列表
		var detail_modal = $('#cancel_order');//取消订单modal

		general.delegate('.click_cancel','click',function () {
			detail_modal.modal();
			click_cancel_order_at_consume($(this));//点击购买模态框中 确定 按钮 发起请求 返回结果  ajax
		})
	}

	//点击购买模态框中 确定 按钮 发起请求 返回结果  ajax
	function click_cancel_order_at_consume($btn_this) {
		var cancel_order = $('#cancel_order');//取消订单modal
		var btn = cancel_order.find('div[data-dismiss="modal"]');

		btn.unbind('click');
		btn.on('click',function () {
			$.ajax({
				url: '/index/cancel_order',
				type:'POST',
				dataType: 'json',
				data: {
						order_id: $btn_this.attr('order_id')
				},
				success:function (data) {
					if( data.code == '1' ){
						$btn_this.parents('.active').eq(0).addClass('define_active');
						$btn_this.parents('.active').eq(0).removeClass('active');
						$btn_this.parents('.active').eq(0).find('.clearfix').eq(0).find('.pull-right').text('已取消');
					}else {
						console.log(data.msg);
					}
				},
				error:function (err) {
					console.log(err);
				}
			})
		})
	}

	/************************************模态框*************************************/
	//show  模态框
	function show_modal($modal) {
		if( !$modal.is(':animated') ){
			$('#mark').css('display','block');
			$modal.addClass('active');
			$modal.animate({
				top: $(window).height() - $modal.height(),
				bottom: 0
			},500);
		}
	}

	//hide  模态框
	function hide_modal($modal) {
		var font_size = $('html').attr('style').split(' ')[1].split('px')[0];
		var dpr = $('html').attr('data-dpr');
		$modal.animate({
			top: $(window).height() + ( font_size / dpr )
		},500,function () {
			$('#mark').css('display','none');
			$modal.removeClass('active');
			//$modal.css('bottom','0');
		});
	}

	//点击背景 或者.modal_close  关闭模态框
	click_hide_modal( $('.modal_close') );
	function click_hide_modal($btn) {
		$btn.on('click',function () {
			var modal = $(this).parents('.modal_define');
			hide_modal( modal );
		});
	}

	/************************************套餐内容*************************************/
	//项目列表  点击套餐内容  展示模态框
	click_meal_content('experience_general');//体验券
	click_meal_content('recharge_status_general');//普通项目
	click_meal_content('recharge_status_gold');//黄金项目
	click_meal_content('recharge_status_platina');//白金项目
	click_meal_content('recharge_status_jewel');//钻石项目
	click_meal_content('recharge_status_mouth');//口腔项目
	function click_meal_content(recharge_status) {
		var general = $('#' + recharge_status);//项目列表
		var myModal = $('#myModal');
		general.delegate('.meal_content','click',function () {
			var select_meal = $('#select_meal');
			var deal_form = $('#deal_form');
			var self = $(this);
			$.ajax({
				url: '/index/getContent',
				type:'POST',
				dataType: 'json',
				data: {
					order_id: self.attr('order_id'),
					project_id: self.attr('project_id')
				},
				success:function (data) {
					if( data.code == '1' ){
						select_meal.show();
						$('#sure_next').parent().show();
						deal_form.hide();

						if( data.data ){
							create_meal_list(data.data);//生成套餐内容列表
						}else {
							myModal.find('.modal-body').text( '暂无套餐内容' );
							myModal.modal({keyboard:false});
						}
					}
				},
				error:function (err) {
					console.log(err);
				}
			});
		})
	}

	//生成套餐内容列表
	function create_meal_list(data) {
		/*
		* is_used  0:未消耗  1：已消耗
		* */
		$('#select_meal').empty();
		for ( i = 0 , y = data.length ; i < y ; i++ ){
			var status = '';
			if( data[i].is_used == '0' ){
				status = '<img src="/statics/images/unselected.png" alt="">';
			}

			if( data[i].is_used == '1' ){
				status = '已消耗';
			}

			var html = '';
			html += '<div class="clearfix" card_id="'+ data[i].card_id +'">';
			html += '<div class="pull_left">'+ data[i].goods_name +'</div>';
			html += '<div class="pull_right">'+ status +'</div>';
			html += '</div>';

			$('#select_meal').append( html );
		}

		$('#modal_meal').modal();//套餐内容modal
	}

	//选择套餐列表
	select_meal_item();
	function select_meal_item() {
		$('#select_meal').delegate('.clearfix','click',function () {
			var self = $(this);
			if( self.find('img').length > 0 ){
				if( self.find('img').attr('src') == '/statics/images/unselected.png' ){
					self.find('img').attr('src','/statics/images/selected.png');
					self.addClass('active');
				}else {
					self.find('img').attr('src','/statics/images/unselected.png');
					self.removeClass('active');
				}
			}
		});
	}
	
	//点击 下一步 按钮
	sure_next();
	function sure_next() {
		$('#sure_next').unbind('click');
		$('#sure_next').on('click',function () {
			var select_meal = $('#select_meal');
			var deal_form = $('#deal_form');
			var img_len = select_meal.find('img').length;
			var active_len = select_meal.find('.active').length;

			if( img_len > 0 && active_len > 0 ){
				select_meal.hide();
				$('#sure_next').parent().hide();
				deal_form.show();
			}else {
				select_tip(img_len,active_len);//监测是否有选择项及项目列表消耗完毕提示
			}
		});
	}

	//点击 确认消耗 按钮
	sure_consume();
	function sure_consume() {
		var sure_consume = $('#sure_consume');
		var select_meal = $('#select_meal');
		var deal_form_input = $('#deal_form_input');
		var input = deal_form_input.find('input');
		var myModal = $('#myModal');

		sure_consume.unbind('click');
		sure_consume.on('click',function () {
			if( input.eq(0).val() && input.eq(2).val() ){
				var list = select_meal.find('.active');
				var str = '';
				list.each(function (index,item) {
					str += $(item).attr('card_id') + ',';
					return str;
				});
				sure_consume_ajax(str);//确认消耗  ajax
			}
			else {
				if( !input.eq(0).val() ){
					myModal.find('.modal-body').text( '请输入操作护士' );
					myModal.modal({keyboard:false});
					change_z_index_modal( myModal );//修改modal z-index
					return;
				}

				/*if( !input.eq(1).val() ){
					myModal.find('.modal-body').text( '请输入操作医生' );
					myModal.modal({keyboard:false});
					change_z_index_modal( myModal );//修改modal z-index
					return;
				}*/


				if( !input.eq(2).val() ){
					myModal.find('.modal-body').text( '请输入操作地点' );
					myModal.modal({keyboard:false});
					change_z_index_modal( myModal );//修改modal z-index
					return;
				}
			}
		});
	}

	//确认消耗  ajax
	function sure_consume_ajax(str) {
		$.ajax({
			url: '/index/useGood',
			type:'POST',
			dataType: 'json',
			data: {
				card_id: str,
				operate_nurse: $('#deal_form_input').find('input[name="operate_nurse"]').val(),
				operate_doctor: $('#deal_form_input').find('input[name="operate_doctor"]').val(),
				operate_place: $('#deal_form_input').find('input[name="operate_place"]').val()
			},
			success:function (data) {
				if( data.code == '1' ){
					var myModal = $('#myModal');
					myModal.find('.modal-body').text( '消费成功' );
					myModal.modal({keyboard:false});
					change_z_index_modal( myModal );//修改modal z-index
					myModal.on('hide.bs.modal',function () {
						$('#modal_meal').modal('hide');
						$(this).unbind('hide.bs.modal');
					});
				}
			},
			error:function (err) {
				console.log(err);
			}
		});
	}

	//监测是否有选择项及项目列表消耗完毕提示
	function select_tip(img_len,active_len) {
		var myModal = $('#myModal');

		if( img_len <= 0 ){
			myModal.find('.modal-body').text( '您的套餐已消耗完' );
			myModal.modal({keyboard:false});
			change_z_index_modal( myModal );//修改modal z-index
		}else if( active_len <= 0 ){
			myModal.find('.modal-body').text( '请您选择套餐内容' );
			myModal.modal({keyboard:false});
			change_z_index_modal( myModal );//修改modal z-index
		}
	}

	//修改modal z-index
	function change_z_index_modal($modal) {
		$modal.css('zIndex', '1052');
		$('body').find('.modal-backdrop:last-child').css('zIndex','1051');
		$('body').find('.modal-backdrop:nth-last-child(2)').css('zIndex','1040');
	}

	/************************************返回上一页***************************/
	//返回上一页
	function go_back() {
		history.go(-1);
	}

	//点击返回键  返回上一页
	click_back_btn();
	function click_back_btn() {
		$('#go_back').on('click',function () {
			go_back();//返回上一页
		});
	}
});